﻿using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.OptimizeConfigModel;
using System;
using System.Linq;

namespace LdtJudge.DataEngine.Common.ConfigXmlLoad
{
    /// <summary>
    /// 数据选择接口
    /// </summary>
    public class OptimizeSourceSerializable : OptimizeSourceSelectInterface
    {
        /// <summary>
        ///  数据优选通用
        /// </summary>
        /// <param name="dydata">动态数据</param>
        /// <param name="xmlpath">优选配置路径文件</param>
        public void OptimizeCommonSource(DataContext dydata, string xmlpath)
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(xmlpath))
            {
                var type = typeof(OptimizeFactConfig);
                System.Xml.Serialization.XmlSerializer xmlSerializer = System.Xml.Serialization.XmlSerializer.FromTypes(new[] { type }).FirstOrDefault();
                var result = xmlSerializer.Deserialize(reader);
                dydata.DataLayerSys.optimizeFactConfig = result as OptimizeFactConfig;
            }
        }
        /// <summary>
        ///  数据优选通用
        /// </summary>
        /// <param name="xmlpath">优选配置路径文件</param>
        public OptimizeFactConfig OptimizeCommonSource(string xmlpath)
        {
            try
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(xmlpath))
                {
                    var type = typeof(OptimizeFactConfig);
                    System.Xml.Serialization.XmlSerializer xmlSerializer = System.Xml.Serialization.XmlSerializer.FromTypes(new[] { type }).FirstOrDefault();
                    var result = xmlSerializer.Deserialize(reader);
                    return (result as OptimizeFactConfig);
                }
            }
            catch (Exception)
            {

                return null;
            }
            
        }
        /// <summary>
        /// 获取优选/同节点合并配置接口
        /// </summary>
        /// <param name="xmlconfig"></param>
        /// <returns></returns>
        public OptimizeFactConfig OptimizeCommonSourceContxt(string xmlconfig) 
        {
            try
            {
                OptimizeFactConfig optimizeFactConfig = XmlUtility.XmlUtility.DeserializeToObject<OptimizeFactConfig>(xmlconfig); 
                return optimizeFactConfig;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        /// <summary>
        /// 数据优选特殊
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public void OptimizeSpecial()
        {
            throw new NotImplementedException();
        }
    }
}
